package com.pingtai.user.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.user.entity.User;
import com.pingtai.user.service.UserManagerService;

public class Login   {
	
	public String j_username;
	public String j_password;
	public String validateCode;
	private String node="";
	private String purview="{}";
	
	
	
	public String getPurview() {
		return purview;
	}

	public void setPurview(String purview) {
		this.purview = purview;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public UserManagerService userManagerService;
	
	
	
	
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public String getJ_username() {
		return j_username;
	}

	public void setJ_username(String j_username) {
		this.j_username = j_username;
	}

	public String getJ_password() {
		return j_password;
	}

	public void setJ_password(String j_password) {
		this.j_password = j_password;
	}

	public String login(){
		String rightValidateCode = (String) ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if(validateCode!=rightValidateCode) return "validateCodeError";
		return "success";
	}
	
	public String toLogin(){
		return "success";
	}
	
	public String index(){
		userManagerService.updateUserLoginInfo();
		List indentTypeList = userManagerService.getIndentTypeList();
		Map indentTypeClassifyMap = userManagerService.getIndentTypeMap(indentTypeList);
		node = "{";
		Set keySet = indentTypeClassifyMap.keySet();
		Iterator it = keySet.iterator();
		int code = 0;
		while(it.hasNext()){
			String key = (String) it.next();
			List typeList = (List) indentTypeClassifyMap.get(key);
			node += "\""+key+"\""+":[";
			for(int i=0;i<typeList.size();i++){
				UserBaseIndentType obj = (UserBaseIndentType) typeList.get(i);
				String produceType = "";
				
//				if(obj.getEnable()!=1){
//					produceType = "<span style='color:red;'>×</span>";
//					obj.setUrl("indent/produceDeveloping");
//				}
				
				if(i==typeList.size()-1){
					node+="{\"menuUrl\":\""+obj.getUrl()+"\",\"name\":\""+obj.getName()+produceType+"\",\"code\":\""+code+"\"}";
				}else{
					node+="{\"menuUrl\":\""+obj.getUrl()+"\",\"name\":\""+obj.getName()+produceType+"\",\"code\":\""+code+"\"},";
				}
				code++;
			}
			if(it.hasNext()){
				node += "],";
			}else{
				node += "]";
			}
			
		}
		node += "}";
		
		User cUser = userManagerService.findCurrectUser();
		if(!userManagerService.isAudit(cUser)){
			purview = "{\"childrenMenus\":[{\"childrenMenus\":[{\"childrenMenus\":[],\"id\":39,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"代理管理\",\"menuType\":2,\"menuUrl\":\"user/userManagerUI?currentPage=1\",\"methods\":\"__MENU,add,products,batchProducts,reset,query,recharge,withhold,activate\",\"parentMenu\":{\"childrenMenus\":[],\"id\":34,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[],\"id\":40,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"产品默认价格\",\"menuType\":2,\"menuUrl\":\"user/toDefaultPrice\",\"methods\":\"__MENU,add,products,batchProducts,reset,query,recharge,withhold,activate\",\"parentMenu\":{\"childrenMenus\":[],\"id\":34,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":34,\"listOrder\":99,\"menuLevel\":1,\"menuName\":\"客户管理\",\"menuType\":2,\"menuUrl\":\"#usermanage\",\"methods\":\"__MENU\",\"parentMenu\":{\"childrenMenus\":[],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[{\"childrenMenus\":[],\"id\":44,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"充值消费记录\",\"menuType\":2,\"menuUrl\":\"financial/toConsumptionRecords\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":37,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":37,\"listOrder\":99,\"menuLevel\":1,\"menuName\":\"财务概况\",\"menuType\":2,\"menuUrl\":\"#financerecord\",\"methods\":\"__MENU\",\"parentMenu\":{\"childrenMenus\":[],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"根菜单\",\"menuType\":0,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}";
		}
		
		if(userManagerService.isUser_2(cUser)){
			purview = "{\"childrenMenus\":[{\"childrenMenus\":[{\"childrenMenus\":[],\"id\":44,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"充值消费记录\",\"menuType\":2,\"menuUrl\":\"financial/toConsumptionRecords\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":37,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":37,\"listOrder\":99,\"menuLevel\":1,\"menuName\":\"财务概况\",\"menuType\":2,\"menuUrl\":\"#financerecord\",\"methods\":\"__MENU\",\"parentMenu\":{\"childrenMenus\":[],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"根菜单\",\"menuType\":0,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}";
		}
		
		if(userManagerService.isRoot(cUser)||userManagerService.isAdmin(cUser)){
			purview = "{\"childrenMenus\":[{\"childrenMenus\":[{\"childrenMenus\":[],\"id\":39,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"代理管理\",\"menuType\":2,\"menuUrl\":\"user/userManagerUI?currentPage=1\",\"methods\":\"__MENU,add,products,batchProducts,reset,query,recharge,withhold,activate\",\"parentMenu\":{\"childrenMenus\":[],\"id\":34,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[],\"id\":40,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"产品默认价格\",\"menuType\":2,\"menuUrl\":\"user/toDefaultPrice\",\"methods\":\"__MENU,add,products,batchProducts,reset,query,recharge,withhold,activate\",\"parentMenu\":{\"childrenMenus\":[],\"id\":34,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":34,\"listOrder\":99,\"menuLevel\":1,\"menuName\":\"客户管理\",\"menuType\":2,\"menuUrl\":\"#usermanage\",\"methods\":\"__MENU\",\"parentMenu\":{\"childrenMenus\":[],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[{\"childrenMenus\":[],\"id\":44,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"充值消费记录\",\"menuType\":2,\"menuUrl\":\"financial/toConsumptionRecords\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":37,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":37,\"listOrder\":99,\"menuLevel\":1,\"menuName\":\"财务概况\",\"menuType\":2,\"menuUrl\":\"#financerecord\",\"methods\":\"__MENU\",\"parentMenu\":{\"childrenMenus\":[],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[{\"childrenMenus\":[],\"id\":51,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"用户管理\",\"menuType\":2,\"menuUrl\":\"financial/toConsumptionRecords\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":50,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[],\"id\":52,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"公告管理\",\"menuType\":2,\"menuUrl\":\"noticeManage/noticeManageUI?currentPage=1\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":50,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[],\"id\":53,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"成本价设置\",\"menuType\":2,\"menuUrl\":\"produceManage/produceManageUI?currentPage=1\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":50,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[],\"id\":54,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"提示信息\",\"menuType\":2,\"menuUrl\":\"produceManage/pointOutManageUI?currentPage=1\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":50,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}},{\"childrenMenus\":[],\"id\":55,\"listOrder\":99,\"menuLevel\":2,\"menuName\":\"COOKIE管理\",\"menuType\":2,\"menuUrl\":\"cookieManage/cookieManageUI?currentPage=1\",\"methods\":\"__MENU,query\",\"parentMenu\":{\"childrenMenus\":[],\"id\":50,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":50,\"listOrder\":99,\"menuLevel\":1,\"menuName\":\"后台管理\",\"menuType\":2,\"menuUrl\":\"#backmanage\",\"methods\":\"__MENU\",\"parentMenu\":{\"childrenMenus\":[],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"\",\"menuType\":1,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}}],\"id\":1,\"listOrder\":0,\"menuLevel\":0,\"menuName\":\"根菜单\",\"menuType\":0,\"menuUrl\":\"\",\"methods\":\"\",\"parentMenu\":null}";
		}
		
		return "success";
	}

}
